﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class ShipFirePackage
    {
        public ShipUnit target;
        public CommonAnimCurve fireData;
        public ShipFirePackage(ShipUnit target, CommonAnimCurve fireData)
        {
            this.target = target;
            this.fireData = fireData;
        }
    }

    public enum BulletState
    {
        Fire,
        Fly,
        None
    }

    public class ShipFireState : Yunchang.FsmStateBase<ShipState>
    {
        private ShipBehaviourCtr ctr;
        private ShipFirePackage firePakage;

        private BulletState state;
        private Transform fireRoot, bulletRoot;
        private CommonEffect fire, bullet;

        private float epolse = 0;
        public ShipFireState(ShipState stateType, ShipBehaviourCtr controller) : base(stateType, controller)
        {
            ctr = controller; 
        }
        public override void Enter()
        {
            //Debug.LogError(ctr.render.name+"进入发射状态");
            base.Enter();
            epolse = 0;
            firePakage = ctr.firePakage;
            fireRoot = ctr.render.fireBone.transform;
            bulletRoot = ctr.render.bulletBone.transform;
            state = BulletState.Fire;

            //设置开炮起点
            var fireOffsetPos = UtilityHelper.GetShipModelFireOffset(ctr.render.shipUnit.ShipModelId,ctr.render.shipUnit.IsLeft);
            var firePos = ctr.render.transform.TransformPoint(fireOffsetPos);
            fireRoot.transform.position = firePos;
            bulletRoot.transform.position = firePos;
            //获取 地方船受击点
            var hitOffsetPoint = UtilityHelper.GetShipModelHitPos(firePakage.target.ShipModelId);
            var hitPoint = firePakage.target.ShipRenderer.transform.TransformPoint(hitOffsetPoint);

            //设置曲线的起点终点
            firePakage.fireData.startPos = firePos;
            firePakage.fireData.endPos = hitPoint;
        }
        private Vector3 GetPos(float value)
        {
            return firePakage.fireData.EvaluatePosition(value);
        }
        public override void Update()
        {
            if (state == BulletState.Fire)
            {
                fire = CommonEffectManager.Instance.PlayEffectImmediate(CommonEffectResId.AC_Ship_Fire, fireRoot );
                bullet = CommonEffectManager.Instance.PlayEffectImmediate(CommonEffectResId.AC_Ship_Fire_Bullet, bulletRoot , (obj) =>
                {
                    state = BulletState.Fly;
                });
                fireRoot.position = GetPos(0);
                bulletRoot.position = GetPos(0);
            }
            else if (state == BulletState.Fly)
            {
                epolse += Time.unscaledDeltaTime;
                if (epolse > firePakage.fireData.AnimRealTime)
                {
                    var hitData = new HitData(ctr.render.shipUnit, GetPos(1));
                    firePakage.target.OnHit(hitData);
                    ctr.SwitchState(ShipState.Idel);
                    state = BulletState.None;
                    return;
                }

                bulletRoot.position = GetPos(epolse / firePakage.fireData.AnimRealTime);
            }
        }
        public override void Leave()
        {
            fire.Clear();
            bullet.Clear();
            state = BulletState.None;
        }
    }
}
